package com.bgs.travelapp.alipay.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.bgs.travelapp.alipay.service.AlipayService;
import com.bgs.travelapp.result.CommonResult;
import com.bgs.travelapp.util.RanNum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * lzy 2022-8-15 19点40分
 * 支付宝当面付接口
 */
@Service
public class AlipayServiceImpl implements AlipayService {

    /**
     * 连接支付宝当面付接口，传入价格、商品编号返回支付二维码
     * lzy 2022-8-15 19点40分
     * @param price
     * @param ranNum
     * @return
     * @throws AlipayApiException
     */
    @Override
    public Map<String, Object> qecode(Double price,String ranNum) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","2021003143625575","MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCWNKYkOvStg1Z9/BuxltHsGxVWv/43krGcAZ5J3kp1XvXJqiKQueqk1wLWSP3mWg8LjXton0+s1pyxbRmn5dJvAmbsPIAkWC7v6hD+SP3DN6SkHduzbjNGn5pOTpyNHRRb0ZSdZUVjz85pFu9g3L7D8P325770yjYMgeEw79CscvN8ftMUNhJmmVpr0AGuk2i9SKijnhkGW/6TWaLZIE5OiUFq/NsNSMX1iemq2/Gc0CGOM9jmhiwL5y2VSMvU0liZQ0mBI4tx4QY1IZric9y30p0x9/7qu+HvlbVzl2R4jxNlNQ7olfJeQhi43wP21Tqpr2FrGeg58mn2G0r0InspAgMBAAECggEAYTpXSSB4fVCEzixA6OWNoN2vJGZG8kYcfbV2dq5TWEJNq2pb0X7m6ijbh0XcRB2Lm5jLh1+IPYL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Dtt677bbkGoaxL3i0/yRZsQBiJYI1UOONdeE9FbqYoQcjDIN1PkY70uDRgLh19WXTKg+WU=","json","GBK","MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhiNFlWM9Z9oDFSk5UPwnJ00toxO+2EuIvjw/MU3QKJ1ML08IJLZGQVXCPnTV3Yt5ZbDKItEppviPa/ivaTMu6EXlE/BmFHrbIXk4PnAtlckAubDniT1jX7Qgdf+2jiuZIi088JakRJ1qdbHW8cjnCxsRddOd1XrXNHLezMUDayOuSlzanTNwmRJtMh48f4zNXDxCT4GtmR/Ikb8vJUNVsxAPwk3zDaMBdelMT0LfYRITyTl2nYVVZof935eS73o8+j/L8WYwJ1JoARu8KVTBXY2wobaYmkBb9U++hLR7T7HL1Yfpjn/DbTfTM7KaRMuTIaPoCyHW6V6RRKWd9zEwkwIDAQAB","RSA2");
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        request.setNotifyUrl(" http://z4bs4g.natappfree.cc/#/home");
//        request.setReturnUrl(" http://z4bs4g.natappfree.cc/#/home");
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", ranNum);
        bizContent.put("total_amount", price);
        bizContent.put("subject", "测试商品");

        //// 商品明细信息，按需传入
        //JSONArray goodsDetail = new JSONArray();
        //JSONObject goods1 = new JSONObject();
        //goods1.put("goods_id", "goodsNo1");
        //goods1.put("goods_name", "子商品1");
        //goods1.put("quantity", 1);
        //goods1.put("price", 0.01);
        //goodsDetail.add(goods1);
        //bizContent.put("goods_detail", goodsDetail);

        //// 扩展信息，按需传入
        //JSONObject extendParams = new JSONObject();
        //extendParams.put("sys_service_provider_id", "2088511833207846");
        //bizContent.put("extend_params", extendParams);

        //// 结算信息，按需传入
        //JSONObject settleInfo = new JSONObject();
        //JSONArray settleDetailInfos = new JSONArray();
        //JSONObject settleDetail = new JSONObject();
        //settleDetail.put("trans_in_type", "defaultSettle");
        //settleDetail.put("amount", 0.01);
        //settleDetailInfos.add(settleDetail);
        //settleInfo.put("settle_detail_infos", settleDetailInfos);
        //bizContent.put("settle_info", settleInfo);

        //// 二级商户信息，按需传入
        //JSONObject subMerchant = new JSONObject();
        //subMerchant.put("merchant_id", "2088000603999128");
        //bizContent.put("sub_merchant", subMerchant);

        //// 业务参数信息，按需传入
        //JSONObject businessParams = new JSONObject();
        //businessParams.put("busi_params_key", "busiParamsValue");
        //bizContent.put("business_params", businessParams);

        //// 营销信息，按需传入
        //JSONObject promoParams = new JSONObject();
        //promoParams.put("promo_params_key", "promoParamsValue");
        //bizContent.put("promo_params", promoParams);

        request.setBizContent(bizContent.toString());
        AlipayTradePrecreateResponse response = alipayClient.execute(request);
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        Map<String,Object> map = new HashMap<>();
        String outTradeNo = response.getOutTradeNo();
        map.put("code", ranNum);
        map.put("QrCode", response.getQrCode());
        map.put("body", response.getBody());
        return map;
    }

    /**
     * 询问订单状态
     * lzy 2022-8-16 09点46分6
     * @param code
     * @return
     * @throws AlipayApiException
     */
    @Override
    public Map<String, Object> query(String code) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","2021003143625575","MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCWNKYkOvStg1Z9/BuxltHsGxVWv/43krGcAZ5J3kp1XvXJqiKQueqk1wLWSP3mWg8LjXton0+s1pyxbRmn5dJvAmbsPIAkWC7v6hD+SP3DN6SkHduzbjNGn5pOTpyNHRRb0ZSdZUVjz85pFu9g3L7D8P325770yjYMgeEw79CscvN8ftMUNhJmmVpr0AGuk2i9SKijnhkGW/6TWaLZIE5OiUFq/NsNSMX1iemq2/Gc0CGOM9jmhiwL5y2VSMvU0liZQ0mBI4tx4QY1IZric9y30p0x9/7qu+HvlbVzl2R4jxNlNQ7olfJeQhi43wP21Tqpr2FrGeg58mn2G0r0InspAgMBAAECggEAYTpXSSB4fVC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nt6Zr9etHXLNsiAYkgiLnNhtbKCeom6BlPQO8TnDkpCGv/Fj6sOg8d/gCjtn/o7JDtt677bbkGoaxL3i0/yRZsQBiJYI1UOONdeE9FbqYoQcjDIN1PkY70uDRgLh19WXTKg+WU=","json","GBK","MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhiNFlWM9Z9oDFSk5UPwnJ00toxO+2EuIvjw/MU3QKJ1ML08IJLZGQVXCPnTV3Yt5ZbDKItEppviPa/ivaTMu6EXlE/BmFHrbIXk4PnAtlckAubDniT1jX7Qgdf+2jiuZIi088JakRJ1qdbHW8cjnCxsRddOd1XrXNHLezMUDayOuSlzanTNwmRJtMh48f4zNXDxCT4GtmR/Ikb8vJUNVsxAPwk3zDaMBdelMT0LfYRITyTl2nYVVZof935eS73o8+j/L8WYwJ1JoARu8KVTBXY2wobaYmkBb9U++hLR7T7HL1Yfpjn/DbTfTM7KaRMuTIaPoCyHW6V6RRKWd9zEwkwIDAQAB","RSA2");
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        request.setBizContent("{" +
                "  \"out_trade_no\":\"" + code + "\"," +
                "  \"trade_no\":\"\"," +
                "  \"query_options\":[" +
                "    \"trade_settle_info\"" +
                "  ]" +
                "}");
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        HashMap<String, Object> map = new HashMap<>();
        String msg = response.getMsg();
        String tradeStatus = response.getTradeStatus();
        map.put("msg", msg);
        map.put("tradeStatus", tradeStatus);
        return map;
    }
}
